package com.example.sleepsta;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.sleepsta.app.SleepStaApplication;
import com.example.sleepsta.database.DatabaseHelper;
import com.example.sleepsta.vo.DataItem;

/**
 * 主界面
 * 
 * 判断时间 如果是大于中午12点 启动的时候 结束睡眠的按钮 点击记录 直接出现结果
 * 
 * @author imaginedays
 * 
 */
public class MainActivity extends Activity implements OnClickListener {

	private Button btn_beginTime, btn_endTime;
	private DatabaseHelper dbHelper;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		setUI();
		// 检查当前时间
		// checkCurrentTime();

		dbHelper = new DatabaseHelper(this);
		// dbHelper.clearTableData();
		// read all dataItem
		Log.d(SleepStaApplication.TAG, "reading all data");
		List<DataItem> dataList = dbHelper.getAllItem();
		if (dataList != null && dataList.size() > 0) {
			for (DataItem dataItem : dataList) {
				Log.i(SleepStaApplication.TAG, dataItem.toString());
			}
		}

	}

	private void setUI() {
		btn_beginTime = (Button) findViewById(R.id.btn_record_begin_time);
		btn_beginTime.setOnClickListener(this);
		btn_endTime = (Button) findViewById(R.id.btn_record_end_time);
		btn_endTime.setOnClickListener(this);
	}

	private void checkCurrentTime() {

		Calendar calendar = Calendar.getInstance();
		int curHour = calendar.get(Calendar.HOUR_OF_DAY);

		if (curHour > 12) {
			btn_beginTime.setVisibility(View.INVISIBLE);
		} else {
			btn_endTime.setVisibility(View.INVISIBLE);
		}

		Log.e(SleepStaApplication.TAG, "checkCurrentTime: hour " + curHour);

	}

	/**
	 * type 0 begin type 1 end
	 */
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_record_begin_time:// 开始睡眠
			recordData(0);
			break;

		case R.id.btn_record_end_time:// 结束睡眠
			recordData(1);
			break;
		}

	}

	private void recordData(int type) {
		String curTime = getCurrentTime();
		String curDay = getCurrentDay();

		DataItem dataItem = new DataItem();
		dataItem.setDay(curDay);
		if (type == 0) {
			dataItem.setType(0);
			dataItem.setBeginTime(curTime);
		} else {
			dataItem.setType(1);
			dataItem.setEndTime(curTime);
		}

		if (dbHelper.isAddDataItem(curDay, type)) {// 如果数据库中有数据 则选择更新
			dbHelper.updata(dataItem);
		} else {// 没有这条数据 只能是添加
			dbHelper.addDataItem(dataItem);
		}

	}

	/**
	 * 获得当前时间 精确到 12:30
	 * 
	 * @return
	 */
	private String getCurrentTime() {
		String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
		Log.e(SleepStaApplication.TAG, "getCurrentTime: currentTime "
				+ currentTime);
		return currentTime;
	}

	/**
	 * 获得当前天 精确到 2012-09-20
	 * 
	 * @return
	 */
	private String getCurrentDay() {
		String currentDay = new SimpleDateFormat("yyyy-MM-dd")
				.format(new Date());
		Log.e(SleepStaApplication.TAG, "getCurrentDay: currentDay "
				+ currentDay);
		return currentDay;
	}

}
